#!/bin/bash

REDIS_DIR=/opt/redis
REDIS_BIN=$REDIS_DIR/bin/redis-server
REDIS_CLI=$REDIS_DIR/bin/redis-cli
REDIS_CONF=$REDIS_DIR/etc/redis.conf
REDIS_PASSWORD=$2

start() {
    if [ -f $PIDFILE ]; then
        echo "Redis is already running."
    else
        echo "Starting Redis..."
        $REDIS_BIN $REDIS_CONF
        echo "Redis started."
    fi
}

stop() {
    if [ -f $PIDFILE ]; then
        echo "Stopping Redis..."
        $REDIS_CLI -a "$REDIS_PASSWORD" shutdown
        echo "Redis stopped."
    else
        echo "Redis is not running."
    fi
}

restart() {
    stop
    start
}

status() {
    if [ -f $PIDFILE ]; then
        echo "Redis is running."
    else
        echo "Redis is not running."
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac
